package com.tpp.order.config;

import com.tpp.comm.util.TpConst;
import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {

    /**
     * 订单队列
     * @return
     */
    @Bean
    public Queue orderQueue(){
        return QueueBuilder.durable(TpConst.Rabbit.ORDER_QUEUE)
                .deadLetterRoutingKey(TpConst.Rabbit.ORDER_DEAD_QUEUE)
                .deadLetterExchange("")
                .build();
    }

    /**
     * 订单死信队列
     * @return
     */
    @Bean
    public Queue orderDeadQueue(){
        return QueueBuilder.durable(TpConst.Rabbit.ORDER_DEAD_QUEUE).build();
    }

    /**
     * 订单交换机
     * @return
     */
    @Bean
    public Exchange orderExchange(){
        return ExchangeBuilder.directExchange(TpConst.Rabbit.ORDER_EXCHANGE).build();
    }

    /**
     * 绑定队列和交换机
     * @return
     */
    @Bean
    public Binding orderBinding(){
        return BindingBuilder
                .bind(orderQueue())
                .to(orderExchange())
                .with(TpConst.Rabbit.ORDER_QUEUE)
                .noargs();
    }

    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }

}
